using Exam.BLL;
using Exam.DBHelper;
using Exam.Model;
using ExaminationWeb.Common;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web.Mvc;

namespace ExaminationWeb.Controllers
{
    public class EHSController : Controller
    {
        //
        // GET: /EHS/
        EHSService ehsService = new EHSService();
        public ActionResult EHSList()
        {
            return View();
        }
        public ActionResult EHSAdd()
        {
            return View();
        }
        public ActionResult EHSUpdate(string id)
        {
            ViewData["EHS"] = ehsService.LoadEntites(e => e.EHSID == Guid.Parse(id)).First();
            return View();
        }

        #region 页面事件
        public JsonResult GetAllInfos()
        {
            int pageIndex = Request["sEcho"] == null ? 0 : int.Parse(Request["sEcho"]);
            int pageSize = Request["iDisplayLength"] == null ? 15 : int.Parse(Request["iDisplayLength"]);
            int pageStart = Request["iDisplayStart"] == null ? 0 : int.Parse(Request["iDisplayStart"]);

            string SortDirindex = Request["iSortCol_0"] == null ? "" : Request["iSortCol_0"];
            string SortDir = "";
            string DataProp = "";
            if (!string.IsNullOrEmpty(SortDirindex))
            {
                DataProp = Request["mDataProp_" + SortDirindex] == null ? "" : Request["mDataProp_" + SortDirindex];
                SortDir = Request["sSortDir_0"] == null ? "" : Request["sSortDir_0"]; //asc or desc
            }

            int totalcount = 0;
            string isactive = Request["isactive"] == null ? "-1" : Request["isactive"];
            IQueryable<EHS> ehslist;

            if (isactive == "-1")
            {
                ehslist = ehsService.LoadEntites(e => true, DataProp, SortDir == "asc", pageStart, pageSize, out totalcount);
            }
            else
            {
                ehslist = ehsService.LoadEntites(e => e.IsActive == int.Parse(isactive), DataProp, SortDir == "asc", pageStart, pageSize, out totalcount);
            }

            var results = new { iTotalDisplayRecords = totalcount, iTotalRecords = totalcount, aaData = ehslist, sEcho = 0 };
            return Json(results, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public string AddEHSinfo(EHS ehs)
        {
            try
            {
                if (IsExistData(ehs).EHSID.ToString() == "00000000-0000-0000-0000-000000000000")
                {
                    if (ehs.IsActive == 0)
                    {
                        if (IsExistActiveData().EHSID.ToString() != "00000000-0000-0000-0000-000000000000")
                        {
                            return "activeExist";
                        }
                    }

                    ehs.EHSID = Guid.NewGuid();
                    DateTime time = DateTime.Now;
                    ehs.CreateTime = time;
                    ehs.UpdateTime = time;
                    ehs.CreateUserID = UserAccount.UserID;
                    ehs = ehsService.AddEntity(ehs);
                    return "suc";
                }
                else
                {
                    return "repeat";
                }

            }
            catch (Exception e)
            {
                return "err";
            }
        }
        [HttpPost]
        public string UpdateEHSInfo(EHS ehs)
        {
            try
            {
                EHS _ehs = IsExistData(ehs);
                if (_ehs.EHSID.ToString() == "00000000-0000-0000-0000-000000000000" || _ehs.EHSID == ehs.EHSID)
                {
                    if (ehs.IsActive == 0)
                    {
                        if (IsExistActiveData().EHSID.ToString() != "00000000-0000-0000-0000-000000000000" && ehs.EHSID != IsExistActiveData().EHSID)
                        {
                            return "activeExist";
                        }
                    }

                    ehs.UpdateTime = DateTime.Now;
                    ehs.UpdateUserID = UserAccount.UserID;
                    ehsService.UpdateEntity(ehs);

                    return "suc";
                }
                else
                {
                    return "repeat";
                }
            }
            catch (Exception e)
            {
                return "err";
            }
        }
        #endregion

        #region 公用
        public EHS IsExistData(EHS ehs)
        {
            SQLHelper commonservice = new SQLHelper();
            Dictionary<string, SqlParameter> dic = new Dictionary<string, SqlParameter>();

            SqlParameter parname = new SqlParameter("@EHSContent", System.Data.SqlDbType.NVarChar);
            parname.Value = ehs.EHSContent;
            dic.Add("EHSContent", parname);

            return commonservice.getExistData<EHS>("EHS", dic);
        }

        public EHS IsExistActiveData()
        {
            SQLHelper commonservice = new SQLHelper();
            Dictionary<string, SqlParameter> dic = new Dictionary<string, SqlParameter>();

            SqlParameter parname = new SqlParameter("@IsActive", System.Data.SqlDbType.Int);
            parname.Value = 0;
            dic.Add("IsActive", parname);

            return commonservice.getExistData<EHS>("EHS", dic);
        }
        #endregion
    }
}